package com.capgemini.remind.controller;

import com.capgemini.core.AjaxResponse;
import com.capgemini.core.PageList;
import com.capgemini.interceptor.DealerNumUtils;
import com.capgemini.remind.pojo.*;
import com.capgemini.remind.service.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/report/service")
public class ReportController {
	// 日志记录器
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ReportService reportService;

	@RequestMapping("/report/{dealerNum}")
	public Object report(@PathVariable("dealerNum") String dealerNum1) {
		String dealerNum = DealerNumUtils.getDealerNum();

		ModelAndView mv = new ModelAndView("/remind/report.jsp");

		mv.addObject("dealerNum", dealerNum);
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, -1);;
		mv.addObject("currentMonth",DateFormatUtils.format(ca, "yyyy-MM"));
		return mv;
	}

	@RequestMapping("/returnRateReport/{dealerNum}")
	public Object returnRateReport(@PathVariable("dealerNum") String dealerNum) {

		ModelAndView mv = new ModelAndView("/remind/returnRateReport.jsp");

		mv.addObject("dealerNum", dealerNum);

		return mv;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Object sendHistoryAccount(Report pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		PageList<Report> pl = new PageList<Report>();

		if(!fieldsValidation(pojo)) {
			pl.setItems(new ArrayList<Report>());
			ajaxResponse.setData(pl);
			return ajaxResponse;
		}

		List<Report> rps = null;
		try {
			if("全部".equals(pojo.getMaintenanceItem()))
					pojo.setMaintenanceItem(null);
			rps = reportService.queryReport(pojo, "querySendHistoryAccount");
		} catch(Exception e) {
			log.error("", e);
		}

		pl.setItems(rps);
		
		ajaxResponse.setData(pl);

		return ajaxResponse;
	}

	@RequestMapping("/returnRatelist")
	@ResponseBody
	public Object returnRatelist(Report pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		PageList<Report> pl = new PageList<Report>();
		if(!fieldsValidation(pojo)) {
			pl.setItems(new ArrayList<Report>());
			ajaxResponse.setData(pl);
			return ajaxResponse;
		}

		List<Report> rps = null;
		try {
			if("全部".equals(pojo.getMaintenanceItem()))
				pojo.setMaintenanceItem(null);
			rps = reportService.queryReport(pojo, "queryReturnRatePage");
		} catch (Exception e) {
			log.error("", e);
		}

		pl.setItems(rps);
		ajaxResponse.setData(pl);

		return ajaxResponse;
	}

	private boolean fieldsValidation(Report report){
		if (null != report.getStartDate() && !"".equals(report.getStartDate())
			&& null != report.getEndDate() && !"".equals(report.getEndDate()))
			return true;
		else 
			return false;
	}
}
